package edu.study.user.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import edu.study.api.common.dto.BaseRespDto;
import edu.study.api.common.dto.RespDto;
import edu.study.common.util.cache.Lock;
import edu.study.common.util.cache.LockExecuteCallback;
import edu.study.user.api.DepartmentUserApi;
import edu.study.user.api.dto.DepartmentUserDto;
import edu.study.user.api.req.departmentuser.DepartmentUserReq;
import edu.study.user.api.req.departmentuser.QueryDepartmentUserReq;
import edu.study.user.service.DepartmentUserService;

@RestController
public class DepartmentUserApiImpl implements DepartmentUserApi {

	@Autowired
    private Lock lock;

    @Autowired
    private DepartmentUserService departmentUserService;

	@Override
	public BaseRespDto addDepartmentUser(@RequestBody DepartmentUserReq req) {
		BaseRespDto resp = lock.lockAndExecute(req.getDepartmentNo(), req.get_requestId(), new LockExecuteCallback() {
            @Override
            public BaseRespDto doBusiness() {
                BaseRespDto resp1 = departmentUserService.addDepartmentUser(req);
                return resp1;
            }
        });
		return resp;
	}

	@Override
	public BaseRespDto deleteDepartmentUser(@RequestBody DepartmentUserReq req) {
		BaseRespDto resp = lock.lockAndExecute(req.getDepartmentNo(), req.get_requestId(), new LockExecuteCallback() {
            @Override
            public BaseRespDto doBusiness() {
                BaseRespDto resp1 = departmentUserService.deleteDepartmentUser(req);
                return resp1;
            }
        });
		return resp;
	}

	@Override
	public BaseRespDto modifyDepartmentUser(@RequestBody DepartmentUserReq req) {
		BaseRespDto resp = lock.lockAndExecute(req.getDepartmentNo(), req.get_requestId(), new LockExecuteCallback() {
            @Override
            public BaseRespDto doBusiness() {
                BaseRespDto resp1 = departmentUserService.modifyDepartmentUser(req);
                return resp1;
            }
        });
		return resp;
	}

	@Override
	public RespDto<List<DepartmentUserDto>> queryDepartmentUserList(@RequestBody QueryDepartmentUserReq req) {
		RespDto<List<DepartmentUserDto>> resp = departmentUserService.queryDepartmentUserList(req);
		return resp;
	}
}
